﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class EmailSettingBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEmailSettingDA emaiilSettingDA = DataAccess.CreateEmaiilSettingDA();
        private PubParasInfo pubParasInfo;

        public EmailSettingBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public EmailSettingBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> emaiilSettingIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<EmailSettingInfo> emaiilSettingInfos = new List<EmailSettingInfo>();
                EmailSettingInfo emaiilSettingInfo = null;
                for (i = 0; i < emaiilSettingIDS.Count; i++)
                {
                    emaiilSettingInfo = emaiilSettingDA.GetEmaiilSettingByID(dataBase, tran, emaiilSettingIDS[i], this.dataBaseID[0]);
                    emaiilSettingInfos.Add(emaiilSettingInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (emaiilSettingDA.DeleteEmaiilSetting(dataBase, tran, emaiilSettingIDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < emaiilSettingIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<EmailSettingInfo>(dataBase, tran, emaiilSettingInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "EMAIL_SETTING", this.dataBaseID[0]);
                        temp = temp + emaiilSettingIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return emaiilSettingDA.GetAllEmaiilSetting(this.dataBaseID[0]);
        }

        public object GetInfoByID(string emaiilSettingID)
        {
            return emaiilSettingDA.GetEmaiilSettingByID(emaiilSettingID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            EmailSettingQueryEntity emaiilSettingQuery = obj as EmailSettingQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(emaiilSettingQuery.EmailSettingID))
            {
                sqlWhere.Add(" EMAIL_SETTING_ID " + emaiilSettingQuery.EmailSettingID);
            }
            if (!string.IsNullOrEmpty(emaiilSettingQuery.Description))
            {
                sqlWhere.Add(" DESCRIPTION " + emaiilSettingQuery.Description);
            }
            if (!string.IsNullOrEmpty(emaiilSettingQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + emaiilSettingQuery.IsActive);
            }
            return emaiilSettingDA.GetEmaiilSettingByQueryList(sqlWhere, emaiilSettingQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string emaiilSettingID = string.Empty;
            try
            {
                EmailSettingInfo emaiilSetting = obj as EmailSettingInfo;
                if (emaiilSettingDA.CheckEmaiilSettingIDUnique(emaiilSetting.EmailSettingID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2250_001", this.pubParasInfo.UserLanguage, new List<string> { emaiilSetting.EmailSettingID });
                }
                emaiilSetting.CreatedDate = DateTime.Now;
                emaiilSetting.UpdatedDate = DateTime.Now;
                emaiilSetting.UpdatedBy = emaiilSetting.CreatedBy;
                if (emaiilSettingDA.InsertEmaiilSetting(emaiilSetting, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EmailSettingInfo>(null, emaiilSetting, AuditAction.Insert, this.pubParasInfo.UserID, "EMAIL_SETTING", this.dataBaseID[0]);
                    emaiilSettingID = emaiilSetting.EmailSettingID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return emaiilSettingID;
        }

        public string Update(object obj)
        {
            string emaiilSettingID = string.Empty;
            try
            {
                EmailSettingInfo emaiilSetting = obj as EmailSettingInfo;
                emaiilSetting.UpdatedDate = DateTime.Now;
                EmailSettingInfo oldEmaiilSettingInfo = emaiilSettingDA.GetEmaiilSettingByID(emaiilSetting.EmailSettingID, this.dataBaseID[0]);
                if (emaiilSettingDA.UpdateEmaiilSetting(emaiilSetting, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EmailSettingInfo>(oldEmaiilSettingInfo, emaiilSetting, AuditAction.Update, this.pubParasInfo.UserID, "EMAIL_SETTING", this.dataBaseID[0]);
                    emaiilSettingID = emaiilSetting.EmailSettingID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return emaiilSettingID;
        }
    }
}

